package com.jymj.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by dl on 2017/8/14.
 */
public class ParseUtils {
    
    private final Logger LOGGER = LoggerFactory.getLogger(ParseUtils.class);
    
    public String ToString(Object obj) {
        if (obj == null || obj.toString().trim().equals("") || obj.equals("null")) {
            return "";
        } else {
            return obj.toString();
        }
    }
    
    public Integer ToInt(Object obj) {
        if (obj == null || obj.toString().trim().equals("") || obj.equals("null")) {
            return -1;
        } else {
            return Integer.parseInt(obj.toString());
        }
    }
    
    public Integer ToIntZero(Object obj) {
        if (obj == null || obj.toString().trim().equals("") || obj.equals("null")) {
            return 0;
        } else {
            return Integer.parseInt(obj.toString());
        }
    }
    
    public long ToBigInt(Object obj) {
        if (obj == null || obj.toString().trim().equals("") || obj.equals("null")) {
            return -1;
        } else {
            return Long.parseLong(obj.toString());
        }
    }
    
    public Date ToDate(Object obj) {
        try {
            if (obj == null || obj.toString().trim().equals("") || obj.equals("null")) {
                return new Date(1, 1, 1);
            } else {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                return format1.parse(obj.toString());
            }
        } catch (ParseException e) {
            LOGGER.error(e.getMessage(), e);
            return new Date(1, 1, 1);
        }
    }
    
    public Date ToDateYearMonth(Object obj) {
        try {
            if (obj == null || obj.toString().trim().equals("") || obj.equals("null")) {
                return new Date(1, 1, 1);
            } else {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM");
                return format1.parse(obj.toString());
            }
        } catch (ParseException e) {
            LOGGER.error(e.getMessage(), e);
            return new Date(1, 1, 1);
        }
    }
    
    public String ToDate(Date obj) {
        try {
            if (obj == null || obj.toString().trim().equals("") || obj.toString().trim().equals("null")) {
                return "";
            } else {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                return format1.format(obj);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return "";
        }
    }
    
    public double ToDouble(Object obj) {
        if (obj == null || obj.toString().trim().equals("") || obj.equals("null")) {
            return 0.0;
        } else {
            return Double.parseDouble(obj.toString());
        }
    }
}
